﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Smarts.Controls
{
    public partial class CollectionManager : UserControl
    {
        /// <summary>
        /// Holds the items in the list as a list of objects
        /// </summary>
        protected List<object> _items;

        /// <summary>
        /// The delegate for the <see cref="AddItem"/> event
        /// </summary>
        public delegate void AddObject();

        /// <summary>
        /// The event that is raised when the add button is clicked
        /// </summary>
        public event AddObject AddItem; 
        
        /// <summary>
        /// The image used for the add button. 16 x 16
        /// </summary>
        public Image AddImage
        {
            get
            {
                return addToList.Image;
            }
            set
            {
                addToList.Image = value;
            }
        }

        /// <summary>
        /// The image used for the remove button. 16 x 16
        /// </summary>
        public Image RemoveImage
        {
            get
            {
                return removeFromList.Image;
            }
            set
            {
                removeFromList.Image = value;
            }
        }

        /// <summary>
        /// The image used for the up button. 16 x 16
        /// </summary>
        public Image UpImage
        {
            get
            {
                return moveItemUp.Image;
            }
            set
            {
                moveItemUp.Image = value;
            }
        }

        /// <summary>
        /// The image used for the down button. 16 x 16
        /// </summary>
        public Image DownImage
        {
            get
            {
                return moveItemDown.Image;
            }
            set
            {
                moveItemDown.Image = value;
            }
        }

        /// <summary>
        /// The items in the list
        /// </summary>
        public List<object> Items
        {
            get
            {
                return _items;
            }
            set
            {
                if (value == null)
                {
                    _items = new List<object>();
                }
                else
                {
                    _items = value;
                }
                UpdateList();
            }
        }

        private void UpdateList()
        {
            var itemsAsString = from item in _items
                                select item.ToString();
            collectionItems.Items.Clear();
            foreach (string i in itemsAsString)
            {
                collectionItems.Items.Add(i);
            }
            SynchronizeButtons();
        }

        /// <summary>
        /// Default constructor. Initially the set of items
        /// is empty.
        /// </summary>
        public CollectionManager()
        {
            _items = new List<object>();
            InitializeComponent();
        }

        private void SynchronizeButtons()
        {
            if (collectionItems.SelectedIndex >= 0)
            {
                removeFromList.Enabled = true;
                moveItemUp.Enabled = (collectionItems.SelectedIndex > 0);
                moveItemDown.Enabled = (collectionItems.SelectedIndex < (collectionItems.Items.Count - 1));
            }
            else
            {
                removeFromList.Enabled = false;
                moveItemUp.Enabled = false;
                moveItemDown.Enabled = false;
            }
        }

        private void collectionItems_Click(object sender, EventArgs e)
        {
            SynchronizeButtons();
        }

        private void removeFromList_Click(object sender, EventArgs e)
        {
            _items.RemoveAt(collectionItems.SelectedIndex);
            collectionItems.Items.RemoveAt(collectionItems.SelectedIndex);
            SynchronizeButtons();
        }

        private void moveItemUp_Click(object sender, EventArgs e)
        {
            int newPosition = collectionItems.SelectedIndex -1;
            int oldPosition = collectionItems.SelectedIndex;
            Swap(newPosition, oldPosition);
            collectionItems.SelectedIndex = newPosition;
            SynchronizeButtons();
        }

        private void Swap(int IndexA, int IndexB)
        {
            object objA = _items[IndexA];
            object objB = _items[IndexB];
            _items[IndexA] = objB;
            _items[IndexB] = objA;
            collectionItems.Items[IndexA] = objB.ToString();
            collectionItems.Items[IndexB] = objA.ToString();
        }

        private void moveItemDown_Click(object sender, EventArgs e)
        {
            int newPosition = collectionItems.SelectedIndex + 1;
            int oldPosition = collectionItems.SelectedIndex;
            Swap(newPosition, oldPosition);
            collectionItems.SelectedIndex = newPosition;
            SynchronizeButtons();
        }

        private void addToList_Click(object sender, EventArgs e)
        {
            if (AddItem != null)
            {
                AddItem();
            }
        }

        private void collectionItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            SynchronizeButtons();
        }
    }
}
